package engineer.producers;

import design_patterns.command.CommandQueue;
import entity_model.EntityModel;
import mysql.Connection;
import mysql.Schema;

/**
 * Created by nuno-rafael-figueiredo on 02-07-2014.
 */
public class CreateSchema extends CommandQueue {

    public CreateSchema(Connection connection, EntityModel entityModel) {

        Schema schema;
        if (connection.getSchemaName() == null) {
            add(new mysql.ddl.CreateSchema(connection, entityModel.getTitle().toSnakeCase()));
            schema = new Schema(connection);
        } else
            schema = connection.extract();

        entityModel.entities
                .stream()
                .filter(entity -> schema.getTable(entity.getTitle()) == null)
                .forEach(entity -> add(new CreateTable(entity)));
    }
}
